package com.shoy.myapp.common;

import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;

/**
 * 对话框辅助类
 *
 * @author shay
 * @created 2015/04/03
 */
public class DialogHelper {
    private static CustomDialog mDayeasyDialog;

    /**
     * 显示ProgressDialog
     *
     * @param context 上下文
     * @param message 消息
     */
    public static void loading(Context context, String message) {

        loading(context, message, true);
    }

    /**
     * 显示ProgressDialog
     *
     * @param context 上下文
     * @param message 消息
     */
    public static void loading(Context context, String message, final ILoadingOnKeyListener listener) {
        loading(context, message, true, listener);
    }

    /**
     * 显示ProgressDialog
     *
     * @param context    上下文
     * @param message    消息
     * @param cancelable 是否可以取消
     */
    public static void loading(Context context, String message, boolean cancelable) {

        if (mDayeasyDialog == null) {
            mDayeasyDialog = new CustomDialog(context, message);
            mDayeasyDialog.setCancelable(cancelable);
        }
        if (mDayeasyDialog.isShowing()) {
            mDayeasyDialog.cancel();
            mDayeasyDialog.dismiss();
        }
        mDayeasyDialog.show();
    }

    /**
     * 显示ProgressDialog
     *
     * @param context 上下文
     * @param message 消息
     */
    public static void loading(Context context, String message, boolean cancelable, final ILoadingOnKeyListener listener) {

        if (mDayeasyDialog == null) {
            mDayeasyDialog = new CustomDialog(context, message);
            mDayeasyDialog.setCancelable(cancelable);
        }

        if (mDayeasyDialog.isShowing()) {
            mDayeasyDialog.cancel();
            mDayeasyDialog.dismiss();
        }

        if (null != listener) {
            mDayeasyDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    listener.onKey(dialog, keyCode, event);
                    return false;
                }
            });
        } else {
            mDayeasyDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        mDayeasyDialog.dismiss();
                    }
                    return false;
                }
            });
        }

        mDayeasyDialog.show();
    }

    /**
     * 关闭ProgressDialog
     */
    public static void closeLoading() {
        if (mDayeasyDialog != null) {
            mDayeasyDialog.dismiss();
            mDayeasyDialog = null;
        }
    }

    /**
     * 更新ProgressDialog进度消息
     *
     * @param message 消息
     */
    public static void updateProgressText(String message) {
        if (mDayeasyDialog == null) return;

        if (mDayeasyDialog.isShowing()) {
            mDayeasyDialog.setMessage(message);
        }
    }

    /**
     * Loading监听对话框
     */
    public interface ILoadingOnKeyListener {
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event);
    }
}
